/*
 *------------------------------------------------------------------------- 
 *
 * Demonstration program illustrating the usage
 * of the "Divisorial Matrix".
 * 
 * Written by R.J. Cano (remy@ula.ve) on Jan 31 2013.
 * 
 * Released under the terms of the General Public License GNU-GPL 3.0
 * 
 * WITHOUT ANY WARRANTY WHATSOEVER.
 * 
 * Acknowledgements to:
 * 
 * 	Joerg Arndt (www.jjj.de),
 * 	M. F. Hasler,
 * 	Antti Karttunen,
 * 	Alexander R. Povolotsky
 * 
 * For their valuable help and moral support.
 * 
 *-------------------------------------------------------------------------
 * 
 * Before starting up, a little bit of fun:
 * 
 * "Q: How to find better answers?...  A: "Making better questions!".
 *
 * The palindromic day: ... 31 2013
 * ........................ 31?2013
 * ........................ 3102013
 * ...................................
 * ................................... Let be r a radix, Let's set r=10 for the Decimal system;
 * ...................................
 * ................................... Truncate 3 digits from right: 3102{013} ---> 3102
 * ................................... Truncate 3 digits from left:  {310}2013 ---> 2013
 * ...................................
 * ........................................ Subtract:             -------------------------
 * ........................................ Divide by (r-1):                   ---> 1089 
 * ........................................ Again a palindromic:               --->  121
 * ........................................ It is a perfect square:            ---> 11^2
 * ........................................
 * ........................................ It is written with the same digits!!
 * ........................................ Either as it is or as a perfect square ---> 121, 11^2;
 * ........................................
 * ........................................ It is the product of two primes: ---> 121= 11*11
 * ........................................
 * ........................................ (1+2+1)^(1*2*1)= (1*2*1)^(1+2+1)
 * ........................................
 * ........................................ They are also the product of two (even three) different primes:
 * ........................................ (regardless the exponents!)
 * ........................................   i) 121 - (1+2+1)
 * ........................................  ii) 121 - (1*2*1) = 121 - (1*2*1)!
 * ........................................ iii) 121 - (1+2+1) - (1*2*1)
 * ........................................  iv) 121 - (1+2+1)^(1*2*1) = 121 - (1*2*1)^(1+2+1)
 * ........................................ If it were accidentally deleted a 1 in the 121 of (iii)
 * ........................................   v) 12 - (1+2+1) - (1*2*1)
 * .Have..a..nice..Capicua..day!!!.........  vi) 21 - (1+2+1) - (1*2*1)
 * ........................................ 
 * 
 * 121-(1+2+1)! is prime;
 * -1*matrix(4,4,i,j,(i<=j))*([3,1,0,2]~-[2,0,1,3]~); \\ (PARI/GP) 
 * 
 * Finally, count the dots and the asterisks present inside this comment,,,,,,
 * subtract both counts, the result is a prime!!
 * 
*/ 

default(echo,0);

N=3;                         /* Permutations: 012.....(N-1); Internally and for calculations it is the proper choice. */
radix=10;                    /* The radix where we want to compute the terms (When it applies. See below)             */
diff2nd=0;                   /* 0 for Ui215940, 1 for Ui217626 where Ui stands for "Universal invariant"              */
txtpatchforMD5comparison=0;  /* This is for appending at the end of the output an: [0,0,0,.....,0]~                   */

filename01="/tmp/tmp_pariandmatrices.txt";

op1(n)=matrix(n,n,i,j,(i<=j));

CanoTensorAlgorithm(n)={
  my(A=-1*op1(n), fc=vector(n),j,a=0,p=vector(n,u,u-1),lp=p,k,t);
  print("/****/ diff2nd=",diff2nd,"; r=",radix,"; v=",A*(p~-lp~),"; nil=vectorv(",n,"); lv=v; Q=vectorv(",n,");");
  while ( 1,
    
\\ What to print?? (Enable just one of the following options) \\

\\ Option 0: Universal tensor representations. \\
    /* */
    print("lv=if(diff2nd,v,nil); v=",A*(p~-lp~),"; w=(v-lv); print(w);");
    /* */

\\ Option 1: The same than "option 0" and a 2nd column of partial sums. \\
    /* * /
    print("lv=if(diff2nd,v,nil); v=",A*(p~-lp~),"; w=(v-lv); Q+=w; print(w,\" ---> \",Q);");
    / * */
    
\\ Option 2: The polynomial for base-specific values instead \\    
    /* * /
    print("lv=if(diff2nd,v,nil); v=",A*(p~-lp~),"; w=(v-lv); print(sum(l=1,#w,w[l]*r^(#w-l)));");
    / * */
    
\\ Option 3: The same than "option 2" and a 2nd column of partial sums. \\
    /* * /
    print("lv=if(diff2nd,v,nil); v=",A*(p~-lp~),"; w=(v-lv); Q+=w; print(sum(l=1,#w,w[l]*r^(#w-l)),\" \",sum(l=1,#Q,Q[l]*r^(#Q-l)));");
    / * */    
    
    j = 1;
    while ( fc[j] == j,  fc[j]=0;  j+=1; );
      if ( j==n,  if((diff2nd && txtpatchforMD5comparison),print("print(vectorv(",n,"));")); return() );
      fc[j] += 1;
      a = j;
      j += 1;
      k = 1;
      while ( k < j,
	t=p[j]; p[j]=p[k]; p[k]=t;
	k+=1; j-=1;
      );
  );
}

default(echo,0);

CanoTensorAlgorithm(N); \\ Merely an example. By default it should print the first 6 terms of A215940.

quit();

/*
 * Usage (in Unix/Linux bash):
 * 
 * A215940 and A217626 >>> $clear; bgp -q TheDivisorialMatrix.gp | sort | bgp -q
 * 
 * Another sequences   >>> $clear; bgp -q TheDivisorialMatrix.gp | sort | bgp -q
 * 
 * Where "bgp" is an alias for: gp -s 966367642 $*
 * 
*/ 